package com.talkweb.newproject.admin.controller;

import com.talkweb.newproject.admin.rest.SysUserRest;
import com.talkweb.newproject.api.model.common.Response;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import com.talkweb.newproject.api.model.shiro.LoginInfoVo;
import com.talkweb.newproject.api.model.shiro.UserInfo;
import com.talkweb.newproject.api.model.shiro.UserInfoParam;
import com.talkweb.newproject.common.utils.EncryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;

/**
 * Created by qsf on 2019/4/12.
 */
@RestController
@RequestMapping(value="/shiro-api")
@Slf4j
public class ShiroController {
    @Autowired
    private SysUserRest sysUserServiceFeign;

    @RequestMapping(value="/login",method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult<LoginInfoVo> login(@RequestBody UserInfoParam userInfoParam) throws Exception {
        ResponseResult<LoginInfoVo> result  = new ResponseResult<LoginInfoVo>();

        try{
            LoginInfoVo loginInfoVo = new LoginInfoVo();
            String loginName = userInfoParam.getLoginName();
            String password = userInfoParam.getPassword();
            String encryptPwd = EncryptUtil.encryptPsw(password);
            Subject subject = SecurityUtils.getSubject();

            UsernamePasswordToken token = new UsernamePasswordToken(loginName,encryptPwd);
            String message="";
            try{
                subject.login(token);
                message="登录成功";
                log.info(message);

                //设置用户信息
                UserInfo userInfo = new UserInfo();
                BeanUtils.copyProperties(subject.getPrincipal(),userInfo);

                userInfo.setPassword(null);
                userInfo.setSalt(null);
                userInfo.setToken(subject.getSession().getId().toString());
                loginInfoVo.setUserInfo(userInfo);
                ResponseResult<LoginInfoVo> response=sysUserServiceFeign.getLoginInfoByUserId(userInfo.getUserId().toString());


                if(ResponseResult.isSuccess(response)) {
                    LoginInfoVo loginInfoDTO = response.getData();
                    loginInfoVo.setSysRoleVoList(loginInfoDTO.getSysRoleVoList());
                    loginInfoVo.setRolePermissionVoList(loginInfoDTO.getRolePermissionVoList());
                    loginInfoVo.setSysMenuVoList(loginInfoDTO.getSysMenuVoList());
                }else {
                    String errorMsg  = MessageFormat.format("login.getLoginInfoByUserId.error,userid:{0}",userInfo.getUserId().toString());
                    log.error(errorMsg);
                    throw new Exception(errorMsg);

                }
                result.setData(loginInfoVo);
                result.setStatus(Status.SUCCESS);

            }catch(Exception e) {
                result.setStatus(Status.FAILD);
                message="登录异常";
                log.error(message);
                log.error(e.getMessage(),e);
            }
           result.setMessage(message);
            return result;
        } catch(Exception e) {

            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }



    }

    /**
     * 未登录
     * @return
     */
    @RequestMapping(value="/unlogin",method = {RequestMethod.POST,RequestMethod.GET})
    public Response unlogin() {
        Response result = new Response();
        result.setStatus(Status.FAILD);
        result.setMessage("未登录");
        return result;
    }

    /**
     * 未授权
     * @return
     */
    @RequestMapping(value="/unauth",method = {RequestMethod.POST,RequestMethod.GET})
    public Response unauth() {
        Response result = new Response();
        result.setMessage("未授权");
        result.setStatus(Status.FAILD);
        return result;
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value="/loginout",method = {RequestMethod.POST,RequestMethod.GET})
    public Response loginout() {
        Response result = new Response();
        try{
            Subject subject = SecurityUtils.getSubject();
            subject.logout();
            result.setStatus(Status.SUCCESS);
            result.setMessage("退出登录");
            return result;
        }catch(Exception e) {
            result.setStatus(Status.FAILD);
            result.setMessage("退出失败");
            return result;
        }

    }
}
